import os.path
import sys

import yaml

def replace_values(body, replace):
    for key, value in replace.items():
        if key in body:
            if isinstance(body[key], dict):
                replace_values(body[key], value)
            else:
                body[key] = value


def main():
    args = sys.argv[1:]
    if len(args) < 2:
        print("usage: python /root/gitlab/values.yaml /tmp/replace.yaml")
        exit(1)

    body_file = args[0]
    replace_file = args[1]
    print(body_file)
    print(replace_file)
    # Read a.yaml
    with open(body_file, 'r') as f:
        body = yaml.safe_load(f)

    # Read b.yaml
    with open(replace_file, 'r') as f:
        replace = yaml.safe_load(f)

    # Replace values in a_data with values from b_data
    replace_values(body, replace)

    # Write the updated a_data to a new YAML file
    print(os.path.join(os.path.dirname(body_file) + "/updated.yaml"))
    with open(os.path.join(os.path.dirname(body_file) + "/updated.yaml"), 'w') as f:
        yaml.dump(body, f, default_flow_style=False)

if __name__ == '__main__':
    main()